package com.atguigu.jxc.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.atguigu.jxc.dao.OverflowListDao;
import com.atguigu.jxc.dao.OverflowListGoodsDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.DamageList;
import com.atguigu.jxc.entity.DamageListGoods;
import com.atguigu.jxc.entity.OverflowList;
import com.atguigu.jxc.entity.OverflowListGoods;
import com.atguigu.jxc.service.OverflowListGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OverflowListGoodsServiceImpl implements OverflowListGoodsService {
    @Autowired
    private OverflowListDao overflowListDao;
    @Autowired
    private OverflowListGoodsDao overflowListGoodsDao;

    @Override
    public ServiceVO saveOverflowListGoods(OverflowList overflowList, String overflowListGoodsStr) {
        ArrayList<OverflowListGoods> overflowListGoodsList  =
                JSON.parseObject(overflowListGoodsStr, new TypeReference<ArrayList<OverflowListGoods>>(){});

        if (overflowList.getOverflowListId() != null) {
            //修改   只能改单价和数量
        }else {
            //保存
            overflowListDao.insertOverflowList(overflowList);
            overflowListGoodsList.stream().forEach(overflowListGoods -> {
                overflowListGoods.setOverflowListId(overflowList.getOverflowListId());
                overflowListGoodsDao.insertOverflowListGoods(overflowListGoods);
            });

        }

        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    @Override
    public Map<String, Object> list(String sTime, String eTime) {
        ArrayList<OverflowList> damageList= overflowListDao.list(sTime,eTime);
        HashMap<String, Object> map = new HashMap<>();
        map.put("rows", damageList);
        return map;

    }

    @Override
    public Map<String, Object> goodsList(Integer overflowListId) {
        List<OverflowListGoods> damageGoodsList = overflowListGoodsDao.goodsListBydamageListId(overflowListId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("rows", damageGoodsList);

        return map;
    }
}
